package Demo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Random;
import java.util.logging.Handler;

//表示每个玩家的手牌
class HandCard{
    private ArrayList<Card> cards;
}
class Card{
    private String suit;
    private String rank;

    public Card(String suit,String rank){
        this.rank=rank;
        this.suit=suit;
    }

    public String getSuit() {
        return suit;
    }

    public void setSuit(String suit) {
        this.suit = suit;
    }

    public String getRank() {
        return rank;
    }

    public void setRank(String rank) {
        this.rank = rank;
    }

    @Override
    public String toString() {
        return this.suit+this.rank;
    }
}

public class Main {
    public static void main(String[] args) {
        //使用ArrayList表示一副扑克牌
        ArrayList<Card> deck = creatDeck();
        System.out.println("原始"+deck);
        //洗牌（标准库）
//        Collections.shuffle(deck);
//        System.out.println("洗完牌"+deck);
        //自己洗牌
        shuffle(deck);
        System.out.println("洗完牌"+deck);

        //发牌，假设有三个人，每个人发5张牌
        ArrayList<ArrayList<Card>> hands = new ArrayList<ArrayList<Card>>();
        ArrayList<HandCard> handCards = new ArrayList<HandCard>();
        //先创建 3 个人手里的牌，每个人发五张牌
        for (int i = 0; i < 3; i++) {
            ArrayList<Card> hand = new ArrayList<Card>();
            hands.add(hand);
        }

        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                //发牌是轮流的过程
                ArrayList<Card>currentHand = hands.get(j);
                Card card = deck.remove(0);
                currentHand.add(card);
            }
        }

        //打印每个人的手牌
        for (int i = 0; i < 3; i++) {
            System.out.println("第"+i+"个人的手牌:"+hands.get(i));
        }
    }
    public static void shuffle(ArrayList<Card> deck){
        //把整个 ArrayList 从后往前遍历
        //针对每次取到一张牌，就生成一个随下标，拿着当前位置的牌和随机下标位置的牌，进行交换
        Random random = new Random();
        for (int i = deck.size()-1; i > 0 ; i--) {
            int j = random.nextInt(deck.size());
            //交换两张牌的位置
            Card tmp = new Card(deck.get(i).getSuit(),deck.get(i).getRank());
            deck.set(i,deck.get(j));
            deck.set(j,tmp);
        }
    }
    public static ArrayList<Card> creatDeck(){
        //创建一副扑克牌
        ArrayList<Card> deck = new ArrayList<Card>();
        String[] suits = {"♠","♥","♦","♣"};
        for (String suit:suits) {
            //在每个花色中，生成对应的点数的牌
            for (int i = 2; i <= 10; i++) {
                Card card = new Card(suit,""+i);
                deck.add(card);
            }
            Card cardA = new Card(suit,"A");
            Card cardJ = new Card(suit,"J");
            Card cardK = new Card(suit,"K");
            Card cardQ = new Card(suit,"Q");
            deck.add(cardJ);
            deck.add(cardQ);
            deck.add(cardK);
            deck.add(cardA);
        }
        return deck;
    }
}
